/*
@author https://yunp.top
 */
@file:JsQualifier("openfl.utils")

package openfl.utils

open external class Future<T>(
    work: () -> T = definedExternally,
    async: Boolean = definedExternally
) {
    companion object {
        fun <T> ofEvents(
            onComplete: (result: T) -> Unit,
            onError: (error: dynamic) -> Unit = definedExternally,
            onProgress: (v: Int, t: Int) -> Unit = definedExternally
        ): Future<T>

        fun withError(error: dynamic): Future<dynamic>
        fun <T> withValue(value: T): Future<T>
    }

    val error: dynamic
    val isComplete: Boolean
    val isError: Boolean
    val value: T

    fun onComplete(listener: (result: T) -> Unit): Future<T>
    fun onError(listener: (error: dynamic) -> Unit): Future<T>
    fun onProgress(listener: (v: Int, t: Int) -> Unit): Future<T>
    fun ready(waitTime: Int = definedExternally): Future<T>
    fun result(waitTime: Int = definedExternally): T?
    fun <U> then(next: (v: T) -> Future<U>): Future<U>
}